package com.sinopac.action.TokenStorage;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.aspectj.util.FileUtil;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.TokenStorage.TokenVO;
import com.sinopac.model.TokenStorage.TokenStorageModel;
import com.sinopac.util.FileUploadUtil;

@Namespace("/TokenStorage")
@ParentPackage("struts-default")
//@SuppressWarnings("serial")
public class TokenStorageAction extends ActionCore{
	
	private static final long serialVersionUID = 1L;
	private File file;
	private TokenVO tokenVO;
	private String fileContentType;
    private String fileFileName;
    private String error_msg;
    private boolean successFlag = false;
	
	@Action(value="index",results={@Result(name="input",type="dispatcher",location="/form/TokenStorage/TokenStorage.jsp")})
	public String index(){
		TokenStorageModel tsm = new TokenStorageModel();
		tokenVO = tsm.getItems();
		return INPUT;
	}
	
	@Action(value="uploadFile",interceptorRefs={@InterceptorRef(value="fileUpload",params={"allowedTypes","text/xml"}),
			  									@InterceptorRef(value="defaultStack")},
			  				   results={@Result(name="input",location="/form/TokenStorage/TokenStorage.jsp")},
			  				   params={"savePath","/upload"})
	public String uploadFile(){
		if(!StringUtils.equals("text/xml", fileContentType)){
			addFieldError("error_msg", "檔案格式上傳錯誤");
			return INPUT;
		}
		try {
			File copyFile = FileUploadUtil.getUploadUrl(fileFileName);
			FileUtil.copyFile(file, copyFile);
			TokenStorageModel tsm = new TokenStorageModel();
			if(tsm.chkFile_status(copyFile, (String)session.get(USER_NAME))==false){
//				System.out.println("檔案筆數與內容不合上傳失敗");
				addFieldError("error_msg", "檔案筆數與內容不合上傳失敗");
				return INPUT;
			}else{
				successFlag = true;
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		file.delete();
		return INPUT;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public TokenVO getTokenVO() {
		return tokenVO;
	}

	public void setTokenVO(TokenVO tokenVO) {
		this.tokenVO = tokenVO;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getError_msg() {
		return error_msg;
	}

	public void setError_msg(String error_msg) {
		this.error_msg = error_msg;
	}

	public boolean isSuccessFlag() {
		return successFlag;
	}

	public void setSuccessFlag(boolean successFlag) {
		this.successFlag = successFlag;
	}
}
